# import base64
# import time
# from io import BytesIO
#
# from PIL import Image
# from selenium.webdriver import ActionChains, DesiredCapabilities
# import undetected_chromedriver as uc
# from selenium.webdriver.chrome.options import Options
# from selenium.webdriver.support.wait import WebDriverWait
#
# from eshoputils.browser_utils import getEleWait, getElesNoWait, getEleNoWait
# from eshoputils.utils import get_outlook_verify_code, detect_displacement
#
#
# class ShopeeSpider:
#     def __init__(self, urls):
#         self.urls = urls
#
#     def enter_shop(self):
#         all_product_paths = []
#         for url in self.urls:
#             self.browser.execute_script(f'window.open("{url[0]}")')
#             time.sleep(5)
#             login_button = getEleWait(self,'//button[@class="vvOL40 iesrPs AsFRg8 qCI4rz ZKayWA AnY7KS"]')
#             if login_button:
#                 print("需要登陆")
#                 username_input = getEleWait(self,'//*[@id="main"]/div/div[2]/div/div/div/div[2]/div/div[2]/form/div[1]/div[1]/input')
#                 password_input = getEleWait(self,'//*[@id="main"]/div/div[2]/div/div/div/div[2]/div/div[2]/form/div[2]/div[1]/input')
#                 time.sleep(3)
#                 username_input.send_keys("jsxlavpgts@outlook.com")
#                 password_input.send_keys("Aa123456")
#                 print("登陆按钮点击")
#                 login_button.click()
#                 time.sleep(2)
#                 self.browser.refresh()
#                 email_verify_button = getEleWait(self,'//button[@aria-label="Verify by Email OTP"]')
#                 if email_verify_button:
#                     print("需要邮箱校验")
#                     email_verify_button.click()
#                     email_continue_button = getEleWait(self,'//div[@class="DD5hWO jHpdhI nWIhrW"]')
#                     while email_continue_button:
#                         print("邮箱校验前的机器人滑动校验")
#                         drag_pic = getEleWait(self,'//img[@class="wFUSCy" and @draggable="true"]')
#                         background_pic = getEleWait(self,'//img[@class="QxZbFO" and @draggable="false"]')
#                         drag_pic_base64 = drag_pic.get_attribute("src")
#                         background_pic_base64 = background_pic.get_attribute("src")
#                         drag_pic_base64 = drag_pic_base64.split(";")[1].split(",")[1]
#                         background_pic_base64 = background_pic_base64.split(";")[1].split(",")[1]
#                         drag_img_data = base64.b64decode(drag_pic_base64)
#                         background_pic_data = base64.b64decode(background_pic_base64)
#                         drag_image = Image.open(BytesIO(drag_img_data))
#                         background_image = Image.open(BytesIO(background_pic_data))
#                         drag_image.save(r"C:\Users\18222\Desktop\drag.png")
#                         background_image.save(r"C:\Users\18222\Desktop\background.png")
#                         print(f"拖转图片的xy坐标：{drag_pic.location['x']},{drag_pic.location['y']}")
#                         print(f"背景图片的xy坐标：{background_pic.location['x']},{background_pic.location['y']}")
#                         #计算图片距离后拖动
#                         offset = detect_displacement(r"C:\Users\18222\Desktop\background.png", r"C:\Users\18222\Desktop\drag.png")
#                         print(f"滑动距离为:{offset}")
#                         self.slide_by_offset(email_continue_button, offset)
#                         time.sleep(2)
#                         email_continue_button = getEleWait(self, '//div[@class="DD5hWO jHpdhI nWIhrW"]')
#                     #获取邮件
#                     verify_code_input = getEleWait(self, '//*[@id="main"]/div/div[2]/div/div/div/form/div/div[4]/div/input')
#                     if verify_code_input:
#                         time.sleep(5)
#                         print("拖动校验成功进入邮箱验证码输入部分")
#                         print("登陆邮箱获取校验码")
#                         time.sleep(1)
#                         verify_encode = get_outlook_verify_code()
#                         counter = 0
#                         while not verify_encode:
#                             counter += 1
#                             time.sleep(3)
#                             verify_encode = get_outlook_verify_code()
#                             if counter >=3:
#                                 break
#                         if verify_encode:
#                             print(f'邮箱校验码为{verify_encode}')
#                             verify_code_input.send_keys(verify_encode)
#                         else:
#                             raise Exception("没有收到虾皮校验码无法登陆")
#                         verify_btn = getEleWait(self, "//button[@class='vvOL40 iesrPs AsFRg8 qCI4rz TjDXkH ZKayWA AnY7KS']")
#                         verify_btn.click()
#
#             # origin_window = self.browser.current_window_handle
#             # ActionChains(self.browser).click(login_button).perform()
#             # self.waiter.until(expected_conditions.number_of_windows_to_be(2))
#             # new_window = None
#             # for wh in self.browser.window_handles:
#             #     if wh != origin_window:
#             #         new_window = wh
#             #         break
#             # self.browser.switch_to.window(new_window)
#             # new_waiter = WebDriverWait(self.browser, 10)
#             # input = new_waiter.until(expected_conditions.presence_of_element_located(
#             #     (By.XPATH, '//*[@id="identifierId"]')
#             # ))
#             # input.send_keys("zhupaizhiduozhi")
#             # next = new_waiter.until(expected_conditions.presence_of_element_located(
#             #     (By.XPATH,'//*[@id="identifierNext"]/div/button/span')
#             # ))
#             # next.click()
#             # passinput = new_waiter.until(expected_conditions.presence_of_element_located(
#             #     (By.XPATH, '//*[@id="password"]/div[1]/div/div[1]/input')
#             # ))
#             # time.sleep(2)
#             # passinput.send_keys("Csvcn@112711")
#             # pnext = self.browser.find_element(By.XPATH,'//*[@id="passwordNext"]/div/button/span')
#             # pnext.click()
#             # continuebutton = new_waiter.until(expected_conditions.presence_of_element_located(
#             #     (By.XPATH,'//*[@id="yDmH0d"]/c-wiz/div/div[3]/div/div/div[2]/div/div/button/div[3]')
#             # ))
#             # ActionChains(self.browser).click(continuebutton).perform()
#             # self.browser.switch_to.window(origin_window)
#
#     def slide_by_offset(self, btn, offset):
#         ActionChains(self.browser).click_and_hold(btn).perform()
#         for i in [5] * (offset // 5):
#             ActionChains(self.browser).move_by_offset(i,0).perform()
#         time.sleep(1)
#         ActionChains(self.browser).release(btn).perform()
#
#
